package view;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import model.view.CasillaView;
import controller.Maquina;
import controller.exception.MaquinaInstantiationException;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CargarPremio extends javax.swing.JPanel {
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JButton botonAceptar;
	private static CargarPremio instancia;
	private int cantidadRodillos;
	private Set<CasillaView> casillasView;
	private Map<String,Integer> nombreFrutaCodigo;
	private int posicionCambianteH=90;
	private static final String COMBO_BOX ="ComboBox";
	private JTextField textoMonto;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CargarPremio());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	private CargarPremio() {
		super();
		try {
			cantidadRodillos=Maquina.getInstancia().obtenerCantidadRodillos();
			casillasView=Maquina.getInstancia().obtenerCasillas();
			createMap();
			initGUI();
		} catch (MaquinaInstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static CargarPremio getInstancia()
	{
		if (instancia == null)
			instancia = new CargarPremio();
		return instancia;
	}
	
	private void createMap() {
		nombreFrutaCodigo=new HashMap<String,Integer>();
		for(CasillaView view:casillasView){
			nombreFrutaCodigo.put(view.toString(), view.getCodigo());
		}
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(433, 321));
			this.setLayout(null);
			

			
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Seleccione las Casillas que conforman el Premio...");
				jLabel1.setBounds(12, 12, 315, 29);
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Monto: ");
				jLabel2.setBounds(18, 42, 315, 29);
				textoMonto = new JTextField();
				this.add(textoMonto);
				textoMonto.setBounds(275, 42, 146, 23);
			}
			for (int i=0;i<cantidadRodillos;i++){
				JLabel jLabel3 = new JLabel();
				jLabel3.setText("Posicion " +(i+1));
				jLabel3.setBounds(18, posicionCambianteH, 73, 23);
				ComboBoxModel jComboBoxModel = new DefaultComboBoxModel(Utility.crearOpcionesCasilla(casillasView));
				JComboBox jComboBox = new JComboBox();
				jComboBox.setModel(jComboBoxModel);
				jComboBox.setBounds(275, posicionCambianteH, 200, 23);
				jComboBox.setName(COMBO_BOX+(i+1));
				this.add(jLabel3);
				this.add(jComboBox);
				posicionCambianteH=posicionCambianteH+30;
			}
			
			{
				botonAceptar = new JButton();
				this.add(botonAceptar);
				botonAceptar.setText("Aceptar");
				botonAceptar.setBounds(310, posicionCambianteH+80, 91, 35);
				botonAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt)
					{
						try {
							List<Integer> idsFruta=CargarPremio.makeListCasillas(instancia, cantidadRodillos,nombreFrutaCodigo);
							Float monto=Float.valueOf(textoMonto.getText());
							Maquina.getInstancia().crearPremio(idsFruta, monto);
						} catch (MaquinaInstantiationException e) {
							System.out.println("La maquina ya fue creada");
						}catch (NumberFormatException e){
							System.out.println("El valor deber�a ser Float");
						}
					}

					
				});
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	


	protected static List<Integer> makeListCasillas(CargarPremio instancia2,
			int cantidadRodillos2,Map<String,Integer> frutasCodigo) {
		List<Integer> frutasId=new ArrayList<Integer>();
		Component[] components = instancia2.getComponents();
		for(int i=0;i<cantidadRodillos2;i++){
			String nombreFruta=obtainValueCheckBoxFromPanel(components,COMBO_BOX+(i+1));
			frutasId.add(frutasCodigo.get(nombreFruta));
		}
		return frutasId;
	}

	public static String obtainValueCheckBoxFromPanel(Component[] components,String name){
	        for (int i = 0; i < components.length; i++) {
	            if(name.equals(components[i].getName())){
	            	JComboBox comboBox=(JComboBox) components[i];
	            	return Utility.getSelectedValue(comboBox);
	                
	            }
	        }
	        return null;
	    	
	    }
	
	


}
